package com.qifenqian.turbot.module.presenter

import android.content.Context
import com.qifenqian.turbot.base.BasePresenter
import com.qifenqian.turbot.module.contract.StaffContract
import com.qifenqian.turbot.module.entity.response.StaffEntity
import com.qifenqian.turbot.module.model.StaffModel
import com.qifenqian.turbot.net.ModelCallback

/**
 * @author H
 * @createTime 2018/12/27 11:19
 * @description
 * @see
 * @since
 */
class StaffPresenter(context: Context, view: StaffContract.View) : BasePresenter<StaffContract.View, StaffModel>(context, view), StaffContract.Presenter {

    override fun deleteStaff(id: Int) {
        mModel.deleteStaff(id, object : ModelCallback<Nothing> {
            override fun onSuccess(msg: String?, t: Nothing?) {
                mView?.deleteStaffResult(true, msg = msg)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.deleteStaffResult(true, code, msg)
            }
        })
    }

    override fun editStaff(id: Int, userName: String, userCode: String) {
        mModel.editStaff(id, userName, userCode, object : ModelCallback<Nothing> {
            override fun onSuccess(msg: String?, t: Nothing?) {
                mView?.editStaffResult(true, msg = msg)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.editStaffResult(true, code, msg)
            }
        })
    }

    override fun addStaff(userName: String, userCode: String) {
        mModel.addStaff(userName, userCode, object : ModelCallback<Double> {
            override fun onSuccess(msg: String?, t: Double?) {
                mView?.addStaffResult(true, msg = msg, response = t!!)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.addStaffResult(true, code, msg)
            }
        })
    }

    override fun getStaffList() {
        mModel.getStaffList(object : ModelCallback<List<StaffEntity>> {
            override fun onSuccess(msg: String?, t: List<StaffEntity>?) {
                mView?.getStaffListResult(true, staffEntities = t)
            }

            override fun onFailure(code: Int, msg: String?) {
                mView?.getStaffListResult(false, code, msg)
            }
        })
    }

    override fun createModel(): StaffModel {
        return StaffModel(mContext)
    }
}
